package com.uhu.generator;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateType;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.uhu.exception.BusinessException;

import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liujunjie
 * @description
 * @create 2023-11-23 15:03
 **/
public class GeneratorUtils {
    /**
     * genrator by config
     */
    public static void defaultGenerator(List<String> tableNames) {
        if (CollectionUtil.isEmpty(tableNames)) {
            return;
        }

        String url = SpringUtil.getProperty("spring.datasource.druid.url");
        String username = SpringUtil.getProperty("spring.datasource.druid.username");
        String password = SpringUtil.getProperty("spring.datasource.druid.password");

        String tablePrefix = SpringUtil.getProperty("uhu.generator.tablePrefix");
        String author = SpringUtil.getProperty("uhu.generator.author");

        String packageName = SpringUtil.getProperty("uhu.generator.packageName");
        String moduleName = SpringUtil.getProperty("uhu.generator.moduleName");

        String controllerOutputDir = SpringUtil.getProperty("uhu.generator.controllerOutputDir");
        String serviceOutputDir = SpringUtil.getProperty("uhu.generator.serviceOutputDir");
        String serviceImplOutputDir = SpringUtil.getProperty("uhu.generator.serviceImplOutputDir");
        String entityOutputDir = SpringUtil.getProperty("uhu.generator.entityOutputDir");
        String daoOutputDir = SpringUtil.getProperty("uhu.generator.daoOutputDir");
        String xmlOutputDir = SpringUtil.getProperty("uhu.generator.xmlOutputDir");
        if (!StrUtil.isAllNotEmpty(url, username, password, tablePrefix, author,
                packageName, moduleName, xmlOutputDir, controllerOutputDir, daoOutputDir,
                serviceOutputDir, entityOutputDir)) {
            throw new BusinessException("配置有误");
        }
        String[] tablePrefixArray = tablePrefix.split(",");
        Map<OutputFile, String> pathInfo = new HashMap<>();
        pathInfo.put(OutputFile.controller, controllerOutputDir); // 设置controller生成路径
        pathInfo.put(OutputFile.service, serviceOutputDir); // 设置service生成路径
        pathInfo.put(OutputFile.serviceImpl, serviceImplOutputDir); // 设置serviceImpl生成路径
        pathInfo.put(OutputFile.mapper, daoOutputDir); // 设置mapper生成路径
        pathInfo.put(OutputFile.entity, entityOutputDir); // 设置entity生成路径
        pathInfo.put(OutputFile.xml, xmlOutputDir); // 设置mapperXml生成路径
        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> {
                    builder.author(author) // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                    ;
                })
                .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                    if (typeCode == Types.SMALLINT) {
                        // 自定义类型转换
                        return DbColumnType.INTEGER;
                    }
                    return typeRegistry.getColumnType(metaInfo);

                }))
                .packageConfig(builder -> {
                    builder.parent(packageName) // 设置父包名
                            .moduleName(moduleName) // 设置父包模块名
                            .mapper("dao") // Mapper 包名
                            .pathInfo(pathInfo); //设置生成代码地址
                })
                .strategyConfig(builder -> {
                    builder.addInclude(tableNames) // 设置需要生成的表名
                            .addTablePrefix(tablePrefixArray) // 设置过滤表前缀
                            .controllerBuilder().enableRestStyle()
                            .mapperBuilder().enableMapperAnnotation()
                    ;
                })
                .templateConfig(builder -> {
                    builder.disable(TemplateType.ENTITY)
                            .entity("/templates/entity.java")
                            .service("/templates/service.java")
                            .serviceImpl("/templates/serviceImpl.java")
                            .mapper("/templates/mapper.java")
                            .xml("/templates/mapper.xml")
                            .controller("/templates/controller.java")
                            .build();
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
